/*This file is part of the FEBio Studio source code and is licensed under the MIT license
listed below.

See Copyright-FEBio-Studio.txt for details.

Copyright (c) 2020 University of Utah, The Trustees of Columbia University in 
the City of New York, and others.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.*/

#pragma once
#include <QPushButton>
#include <QMenu>
#include <QComboBox>
#include <PostLib/FEMeshData.h>
#include <PostLib/FEPostModel.h>

// Abstract class for creating the menu of the CDataSelectorButton.
class CDataSelector : public QObject
{
	Q_OBJECT

public:
	CDataSelector();
	virtual ~CDataSelector();

	virtual void BuildMenu(QMenu* menu) = 0;

signals:
	void dataChanged();
};

// generic selector for displaying a list of options
class CGenericDataSelector : public CDataSelector
{
public:
	CGenericDataSelector();

	void AddOption(const QString& opt);

protected:
	void BuildMenu(QMenu* menu) override;

private:
	QStringList	m_ops;
};

// Derived class that selects Time or Steps
class CTimeStepSelector : public CDataSelector
{
public:
	CTimeStepSelector();

	void BuildMenu(QMenu* menu) override;
};

// Derived class that builds the menu of a CDataSelectorButton from the model's data
class CModelDataSelector : public CDataSelector, public Post::FEModelDependant
{
public:
	CModelDataSelector(Post::FEPostModel* fem, Post::Data_Tensor_Type ntype, bool btvec = false);
	~CModelDataSelector();

	void BuildMenu(QMenu* menu) override;

public:
	// inherited from FEModelDependant
	void Update(Post::FEPostModel* pfem) override;

private:
	Post::FEPostModel*		m_fem;
	Post::Data_Tensor_Type	m_class;
	bool m_bvec;
};

// Button with a menu for selecting data fields. 
// The menu is generated by a CDataSelector. 
class CDataSelectorButton : public QPushButton
{
	Q_OBJECT

public:
	CDataSelectorButton(QWidget* parent = 0);
	~CDataSelectorButton();

	// set the data selector
	void SetDataSelector(CDataSelector* dataSelector);

	// return the field ID of the selected menu
	int currentValue() const;

	// set the current field ID
	void setCurrentValue(int n);

	// set the current value based on string
	bool setCurrentValue(const char* sz);

protected slots:
	void onAction(QAction* pa);
	void onDataChanged();

signals:
	void currentValueChanged(int n);

private:
	CDataSelector*	m_src;
	QMenu*			m_menu;
	int				m_currentValue;
};

class CDataFieldSelector : public CDataSelectorButton
{
public:
	CDataFieldSelector(QWidget* parent = nullptr);

	void BuildMenu(Post::FEPostModel* fem, Post::Data_Tensor_Type ntype, bool btvec = false);
};

class CColorMapSelector : public QComboBox
{
public:
	CColorMapSelector(QWidget* parent = nullptr);
};

class CPlotObjectDataSelector : public CDataSelector
{
public:
	CPlotObjectDataSelector(Post::FEPostModel::PlotObject* po);

	void BuildMenu(QMenu* menu) override;

private:
	Post::FEPostModel::PlotObject*	m_po;
};
